package com.eportal.action.background;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.BannerStrategy;
import com.eportal.domain.Sysopt;
import com.eportal.service.BannerStrategyService;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 广告分配资源
 */
public class BannerStrategyAction extends BaseAction implements ModelDriven<BannerStrategy> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Logger log = Logger.getLogger(BannerStrategyAction.class);
	private Map<String, Object> result = new HashMap<String, Object>();
	
    @Autowired
    BannerStrategyService bannerStrategyService;
    /**
     * 分配页面
     *
     * @return
     */
    public String distribute() {
    	String shid = getRequest().getParameter("shid");
    	if (StringUtils.isBlank(shid)) {
			return "error";
		}
    	Map<String, Object>  map = bannerStrategyService.getBySql(Long.parseLong(shid),"1");
    	if (map == null) {
    		return "error";
    	}
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	if (user.getAgtCode().equals(map.get("agt")) || userRole == 1 || userRole == 2 || userRole == 21) {
    		getRequest().setAttribute("ifChange", "yes");
		}else{
			getRequest().setAttribute("ifChange", "no");
		}
    	getRequest().setAttribute("map", map);
		if (userRole == 1 || userRole == 2 || userRole == 21) {
			return "distribute";
		}else if (userRole == 3 || userRole == 4) {
			getRequest().setAttribute("agt", user.getAgtCode());
			return "agtDistribute";
		}
		return null;
    }

    /**
     * 资源分配
     *
     * @return
     */
    public void edit() throws IOException {
    	try {
    		BannerStrategy bannerStrategy = getModel();
    		result = bannerStrategyService.updateMethod(bannerStrategy);
		} catch (Exception e) {
			 result.put("code", 0);
             result.put("msg", "操作失败");
             log.error(this, e);
		}
        responseBody(result);
    }
    
    /**
     * 下架页面
     *
     * @return
     */
    public String offView() {
    	String shid = getRequest().getParameter("shid");
    	if (StringUtils.isBlank(shid)) {
			return "error";
		}
    	Map<String, Object>  map = bannerStrategyService.getBySql(Long.parseLong(shid),"2");
    	if (map == null) {
    		return "error";
		}
    	getRequest().setAttribute("map", map);
    	int userRole =  (Integer) getSession().getAttribute("userRole");
		Sysopt  user= (Sysopt)getSession().getAttribute("user");
		if (userRole == 1 || userRole == 2 || userRole == 21) {
			return "off";
		}else if (userRole == 3 || userRole == 4) {
			getRequest().setAttribute("agt", user.getAgtCode());
			return "agtOff";
		}
		return null;
    }
    
    
    /**
     * 下架资源
     *
     * @return
     */
    public void off() throws IOException {
    	BannerStrategy bannerStrategy = getModel();
    	String offType = getRequest().getParameter("offType");
    	String agtName = getRequest().getParameter("agtName");
    	String comuName = getRequest().getParameter("comuName");
    	result = bannerStrategyService.offMethod(bannerStrategy,agtName,comuName,offType);
    	responseBody(result);
    }

    /**
     * 下架资源
     *
     * @return
     */
    public void offLockBanner() throws IOException {
    	String sid = getRequest().getParameter("sid");
    	result = bannerStrategyService.offLockBannerMethod(sid);
    	responseBody(result);
    }
    /**
	 * 资源报表
	 *
	 * @return
	 */
    public String reports() {
		String shid = getRequest().getParameter("shid");
		if (StringUtils.isBlank(shid)) {
			return "error";
		}
		Map<String, Object>  map = bannerStrategyService.reports(Long.parseLong(shid));
		
		getRequest().setAttribute("map", map);
		return SUCCESS;
	}
	

    @Override
    public BannerStrategy getModel() {
        return bannerStrategy;
    }

    private BannerStrategy bannerStrategy = new BannerStrategy();
}
